package com.yaodian100.ec.giftcard.web;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.giftcard.entity.GiftcardAmount;
import com.yaodian100.ec.giftcard.entity.GiftcardItem;
import com.yaodian100.ec.giftcard.entity.GiftcardMain;
import com.yaodian100.ec.giftcard.entity.GiftcardReceiveItem;
import com.yaodian100.ec.giftcard.entity.GiftcardReceiveMain;
import com.yaodian100.ec.giftcard.service.GiftcardService;
import com.yaodian100.ec.giftcard.service.vo.ReceiveGiftcardVo;

public class GiftcardAction extends ActionSupport implements Preparable {
	/**
	 * serial version
	 */
	private static final long serialVersionUID = 3204487012779625120L;

	@Autowired
	private MenuService menuService;
	@Autowired
	private GiftcardService giftcardService;

	// 發卡作業
	private Menu giftcardQueryStatusMenu;
	private String giftcardNameParam;
	private String applyDateParam;
	private String giftcardQueryStatusParam = "-1";
	private Map<Long, GiftcardMain> giftcardMainMap;
	private List<GiftcardAmount> giftcardAmounts;
	private SimplePager pager;
	private String giftcardName;
	private String rechargeDeadline;
	private String applyUser;
	private String[] amount;
	private String[] count;
	private Long mainId;
	private GiftcardMain giftcardMain;
	private Long amountId;
	private InputStream inputStream;
	private GiftcardAmount giftcardAmount;
	private List<GiftcardItem> giftcardItems;
	private GiftcardItem giftcardItem;

	// 領用作業
	private Menu giftcardReceiveQueryStatusMenu;
	private Long receiveIdParam;
	private String applyDateStart;
	private String applyDateEnd;
	private String receiveDateStart;
	private String receiveDateEnd;
	private String approveDateStart;
	private String approveDateEnd;
	private String giftcardReceiveQueryStatusParam = "-1";
	private String receiveUserParam;
	private String applyUserParam;
	private Map<Long, GiftcardReceiveMain> giftcardReceiveMainMap;
	private List<GiftcardReceiveItem> giftcardReceiveItems;
	private String startGiftcardCode;
	private Integer giftcardCount;
	private String receiveApplyUser;
	private String receiveApplyDesc;
	private List receiveAddShowList;
	private List receiveAddErrList;
	private BigDecimal[] receiveItemAmounts;
	private Integer[] receiveItemCounts;
	private BigDecimal receiveTotalAmount;
	private Long receiveId;
	private GiftcardReceiveMain giftcardReceiveMain;
	private String receiveDisapproveReason;
	private String isApprove;
	private String giftcardId;

	public String queryGiftcardInfo() {
		if (StringUtils.isNotBlank(giftcardId)) {

			try {
				giftcardItem = giftcardService.getGiftcardItemByCardCode(giftcardId);
				giftcardAmount = giftcardService.getGiftcardAmountByAmountId(giftcardItem.getAmountId());
				giftcardMain = giftcardService.get(giftcardAmount.getMainId());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				giftcardItem = null;
				giftcardAmount = null;
				giftcardMain = null;
			}
		}

		return "queryResult";
	}

	@Override
	public void prepare() throws Exception {
		clearMessages();
		clearActionErrors();

		if (giftcardQueryStatusMenu == null) {
			giftcardQueryStatusMenu = menuService.get("Giftcard.QueryStatus");
		}

		if (giftcardReceiveQueryStatusMenu == null) {
			giftcardReceiveQueryStatusMenu = menuService.get("Giftcard.ReceiveQueryStatus");
		}

		if (pager == null) {
			pager = new SimplePager();
		}
	}

	/** 發卡作業Start */
	public String list() throws Exception {
		pager.setPageRecord(20);
		List<Object> resultList = giftcardService.queryGiftcard(giftcardNameParam, applyDateParam,
				giftcardQueryStatusParam, pager);

		giftcardMainMap = (Map) resultList.get(0);
		giftcardAmounts = (List) resultList.get(1);

		return "list";
	}

	public String add() throws Exception {

		return "add";
	}

	public String save() throws Exception {
		Date now = new Date();

		GiftcardMain giftcardMain = new GiftcardMain();
		giftcardMain.setApplyUser(applyUser);
		giftcardMain.setApplyDate(now);
		DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
		giftcardMain.setRechargeDeadline(format.parse(rechargeDeadline + " 23:59:59"));
		giftcardMain.setGiftcardName(giftcardName);

		try {
			giftcardService.generate(giftcardMain, amount, count);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("储存失败");
			return list();
		}

		mainId = giftcardMain.getMainId();
		addActionMessage("储存成功");

		return viewAmount();
	}

	public String viewAmount() throws Exception {
		giftcardMain = giftcardService.get(mainId);
		giftcardAmounts = giftcardService.getGiftcardAmountsByMainId(mainId);

		return "viewAmount";
	}

	public String export() throws Exception {
		try {
			inputStream = giftcardService.exportGiftcard(amountId);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("汇出失败");
			return viewAmount();
		}

		return "export";
	}

	public String warehouse() throws Exception {
		try {
			giftcardService.warehouseGiftcard(amountId);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("入库失败");
		}

		addActionMessage("入库成功");

		return viewAmount();
	}

	public String cancel() throws Exception {
		try {
			giftcardService.cancelGiftcard(amountId);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("作废失败");
		}

		addActionMessage("作废成功");

		return viewAmount();
	}

	public String viewItem() throws Exception {
		giftcardAmount = giftcardService.getGiftcardAmountByAmountId(amountId);
		giftcardMain = giftcardService.get(giftcardAmount.getMainId());

		pager.setPageRecord(20);
		giftcardItems = giftcardService.queryGiftcardItems(amountId, pager);

		return "viewItem";
	}

	/** 發卡作業End */

	/** 領用作業Start */
	public String receiveList() throws Exception {
		pager.setPageRecord(20);
		List<Object> resultList=giftcardService.queryReceiveGiftcard(receiveIdParam,applyDateStart,applyDateEnd
				,receiveDateStart,receiveDateEnd,approveDateStart,approveDateEnd,giftcardReceiveQueryStatusParam
				,receiveUserParam,applyUserParam,pager);
		
		giftcardReceiveMainMap=(Map)resultList.get(0);
		giftcardReceiveItems=(List)resultList.get(1);

		return "receiveList";
	}

	public String receiveAdd() throws Exception {
		startGiftcardCode = null;
		giftcardCount = null;
		this.cleanReceiveAdd();

		return "receiveAdd";
	}

	public String searchReceiveAdd() throws Exception {
		if (!giftcardService.checkGiftcardCode(startGiftcardCode)) {
			addActionError("卡号起始号码错误：无此卡号");
			this.cleanReceiveAdd();
			return "receiveAdd";
		}

		List resultList = giftcardService.queryAddReceiveGiftcard(startGiftcardCode, giftcardCount);
		receiveAddShowList = (List) resultList.get(0);
		receiveAddErrList = (List) resultList.get(1);

		if (receiveAddErrList.size() > 0)
			addActionError("此区间有卡号异常：请重新确认");

		return "receiveAdd";
	}

	public String saveReceiveAdd() throws Exception {
		try {
			ReceiveGiftcardVo receiveGiftcardVo = new ReceiveGiftcardVo();
			receiveGiftcardVo.setReceiveApplyUser(receiveApplyUser);
			receiveGiftcardVo.setStartGiftcardCode(startGiftcardCode);
			receiveGiftcardVo.setGiftcardCount(giftcardCount);
			receiveGiftcardVo.setReceiveApplyDesc(receiveApplyDesc);
			receiveGiftcardVo.setReceiveItemAmounts(receiveItemAmounts);
			receiveGiftcardVo.setReceiveItemCounts(receiveItemCounts);
			receiveGiftcardVo.setReceiveTotalAmount(receiveTotalAmount);
			GiftcardReceiveMain giftcardReceiveMain = giftcardService.receiveGiftcard(receiveGiftcardVo);
			receiveId = giftcardReceiveMain.getReceiveId();

			addActionMessage("储存成功");
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("储存失败");
			this.cleanReceiveAdd();
			return "receiveAdd";
		}

		return viewReceiveItem();
	}

	public String viewReceiveItem() throws Exception {
		giftcardReceiveMain = giftcardService.getGiftcardReceiveMainByReceiveId(receiveId);
		giftcardReceiveItems = giftcardService.getGiftcardReceiveItemsByReceiveId(receiveId);

		return "viewReceiveItem";
	}

	public String exportReceiveItems() throws Exception {
		try {
			inputStream = giftcardService.exportReceiveItems(receiveId);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("汇出失败");
			return viewReceiveItem();
		}

		return "exportReceiveItems";
	}

	public String publish() throws Exception {
		try {
			giftcardService.publishGiftcard(receiveId);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("出库失败");
		}

		addActionMessage("出库成功");

		return receiveList();
	}

	public String receiveApprove() throws Exception {
		giftcardReceiveMain = giftcardService.getGiftcardReceiveMainByReceiveId(receiveId);
		giftcardReceiveItems = giftcardService.getGiftcardReceiveItemsByReceiveId(receiveId);

		return "receiveApprove";
	}

	public String approve() throws Exception {
		try {
			if ("true".equals(isApprove)) {
				giftcardService.approve(receiveId, true, receiveDisapproveReason);
			} else {
				giftcardService.approve(receiveId, false, receiveDisapproveReason);
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("审核失败");
		}

		addActionMessage("审核成功");

		return viewReceiveItem();
	}

	private void cleanReceiveAdd() {
		receiveApplyUser = null;
		receiveApplyDesc = null;
		receiveAddShowList = null;
		receiveAddErrList = null;
	}

	/** 領用作業End */

	// getter & setter
	public Menu getGiftcardQueryStatusMenu() {
		return giftcardQueryStatusMenu;
	}

	public void setGiftcardQueryStatusMenu(Menu giftcardQueryStatusMenu) {
		this.giftcardQueryStatusMenu = giftcardQueryStatusMenu;
	}

	public String getGiftcardNameParam() {
		return giftcardNameParam;
	}

	public void setGiftcardNameParam(String giftcardNameParam) {
		this.giftcardNameParam = giftcardNameParam;
	}

	public String getApplyDateParam() {
		return applyDateParam;
	}

	public void setApplyDateParam(String applyDateParam) {
		this.applyDateParam = applyDateParam;
	}

	public String getGiftcardQueryStatusParam() {
		return giftcardQueryStatusParam;
	}

	public void setGiftcardQueryStatusParam(String giftcardQueryStatusParam) {
		this.giftcardQueryStatusParam = giftcardQueryStatusParam;
	}

	public Map<Long, GiftcardMain> getGiftcardMainMap() {
		return giftcardMainMap;
	}

	public void setGiftcardMainMap(Map<Long, GiftcardMain> giftcardMainMap) {
		this.giftcardMainMap = giftcardMainMap;
	}

	public List<GiftcardAmount> getGiftcardAmounts() {
		return giftcardAmounts;
	}

	public void setGiftcardAmounts(List<GiftcardAmount> giftcardAmounts) {
		this.giftcardAmounts = giftcardAmounts;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public String getGiftcardName() {
		return giftcardName;
	}

	public void setGiftcardName(String giftcardName) {
		this.giftcardName = giftcardName;
	}

	public String getRechargeDeadline() {
		return rechargeDeadline;
	}

	public void setRechargeDeadline(String rechargeDeadline) {
		this.rechargeDeadline = rechargeDeadline;
	}

	public String getApplyUser() {
		return applyUser;
	}

	public void setApplyUser(String applyUser) {
		this.applyUser = applyUser;
	}

	public String[] getAmount() {
		return amount;
	}

	public void setAmount(String[] amount) {
		this.amount = amount;
	}

	public String[] getCount() {
		return count;
	}

	public void setCount(String[] count) {
		this.count = count;
	}

	public Long getMainId() {
		return mainId;
	}

	public void setMainId(Long mainId) {
		this.mainId = mainId;
	}

	public GiftcardMain getGiftcardMain() {
		return giftcardMain;
	}

	public void setGiftcardMain(GiftcardMain giftcardMain) {
		this.giftcardMain = giftcardMain;
	}

	public Long getAmountId() {
		return amountId;
	}

	public void setAmountId(Long amountId) {
		this.amountId = amountId;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public GiftcardAmount getGiftcardAmount() {
		return giftcardAmount;
	}

	public void setGiftcardAmount(GiftcardAmount giftcardAmount) {
		this.giftcardAmount = giftcardAmount;
	}

	public List<GiftcardItem> getGiftcardItems() {
		return giftcardItems;
	}

	public void setGiftcardItems(List<GiftcardItem> giftcardItems) {
		this.giftcardItems = giftcardItems;
	}

	public Menu getGiftcardReceiveQueryStatusMenu() {
		return giftcardReceiveQueryStatusMenu;
	}

	public void setGiftcardReceiveQueryStatusMenu(Menu giftcardReceiveQueryStatusMenu) {
		this.giftcardReceiveQueryStatusMenu = giftcardReceiveQueryStatusMenu;
	}

	public Long getReceiveIdParam() {
		return receiveIdParam;
	}

	public void setReceiveIdParam(Long receiveIdParam) {
		this.receiveIdParam = receiveIdParam;
	}

	public String getApplyDateStart() {
		return applyDateStart;
	}

	public void setApplyDateStart(String applyDateStart) {
		this.applyDateStart = applyDateStart;
	}

	public String getApplyDateEnd() {
		return applyDateEnd;
	}

	public void setApplyDateEnd(String applyDateEnd) {
		this.applyDateEnd = applyDateEnd;
	}

	public String getReceiveDateStart() {
		return receiveDateStart;
	}

	public void setReceiveDateStart(String receiveDateStart) {
		this.receiveDateStart = receiveDateStart;
	}

	public String getReceiveDateEnd() {
		return receiveDateEnd;
	}

	public void setReceiveDateEnd(String receiveDateEnd) {
		this.receiveDateEnd = receiveDateEnd;
	}

	public String getApproveDateStart() {
		return approveDateStart;
	}

	public void setApproveDateStart(String approveDateStart) {
		this.approveDateStart = approveDateStart;
	}

	public String getApproveDateEnd() {
		return approveDateEnd;
	}

	public void setApproveDateEnd(String approveDateEnd) {
		this.approveDateEnd = approveDateEnd;
	}

	public String getGiftcardReceiveQueryStatusParam() {
		return giftcardReceiveQueryStatusParam;
	}

	public void setGiftcardReceiveQueryStatusParam(String giftcardReceiveQueryStatusParam) {
		this.giftcardReceiveQueryStatusParam = giftcardReceiveQueryStatusParam;
	}

	public String getReceiveUserParam() {
		return receiveUserParam;
	}

	public void setReceiveUserParam(String receiveUserParam) {
		this.receiveUserParam = receiveUserParam;
	}

	public String getApplyUserParam() {
		return applyUserParam;
	}

	public void setApplyUserParam(String applyUserParam) {
		this.applyUserParam = applyUserParam;
	}

	public Map<Long, GiftcardReceiveMain> getGiftcardReceiveMainMap() {
		return giftcardReceiveMainMap;
	}

	public void setGiftcardReceiveMainMap(Map<Long, GiftcardReceiveMain> giftcardReceiveMainMap) {
		this.giftcardReceiveMainMap = giftcardReceiveMainMap;
	}

	public List<GiftcardReceiveItem> getGiftcardReceiveItems() {
		return giftcardReceiveItems;
	}

	public void setGiftcardReceiveItems(List<GiftcardReceiveItem> giftcardReceiveItems) {
		this.giftcardReceiveItems = giftcardReceiveItems;
	}

	public String getStartGiftcardCode() {
		return startGiftcardCode;
	}

	public void setStartGiftcardCode(String startGiftcardCode) {
		this.startGiftcardCode = startGiftcardCode;
	}

	public Integer getGiftcardCount() {
		return giftcardCount;
	}

	public void setGiftcardCount(Integer giftcardCount) {
		this.giftcardCount = giftcardCount;
	}

	public String getReceiveApplyUser() {
		return receiveApplyUser;
	}

	public void setReceiveApplyUser(String receiveApplyUser) {
		this.receiveApplyUser = receiveApplyUser;
	}

	public String getReceiveApplyDesc() {
		return receiveApplyDesc;
	}

	public void setReceiveApplyDesc(String receiveApplyDesc) {
		this.receiveApplyDesc = receiveApplyDesc;
	}

	public List getReceiveAddShowList() {
		return receiveAddShowList;
	}

	public void setReceiveAddShowList(List receiveAddShowList) {
		this.receiveAddShowList = receiveAddShowList;
	}

	public List getReceiveAddErrList() {
		return receiveAddErrList;
	}

	public void setReceiveAddErrList(List receiveAddErrList) {
		this.receiveAddErrList = receiveAddErrList;
	}

	public BigDecimal[] getReceiveItemAmounts() {
		return receiveItemAmounts;
	}

	public void setReceiveItemAmounts(BigDecimal[] receiveItemAmounts) {
		this.receiveItemAmounts = receiveItemAmounts;
	}

	public Integer[] getReceiveItemCounts() {
		return receiveItemCounts;
	}

	public void setReceiveItemCounts(Integer[] receiveItemCounts) {
		this.receiveItemCounts = receiveItemCounts;
	}

	public BigDecimal getReceiveTotalAmount() {
		return receiveTotalAmount;
	}

	public void setReceiveTotalAmount(BigDecimal receiveTotalAmount) {
		this.receiveTotalAmount = receiveTotalAmount;
	}

	public Long getReceiveId() {
		return receiveId;
	}

	public void setReceiveId(Long receiveId) {
		this.receiveId = receiveId;
	}

	public GiftcardReceiveMain getGiftcardReceiveMain() {
		return giftcardReceiveMain;
	}

	public void setGiftcardReceiveMain(GiftcardReceiveMain giftcardReceiveMain) {
		this.giftcardReceiveMain = giftcardReceiveMain;
	}

	public String getReceiveDisapproveReason() {
		return receiveDisapproveReason;
	}

	public void setReceiveDisapproveReason(String receiveDisapproveReason) {
		this.receiveDisapproveReason = receiveDisapproveReason;
	}

	public String getIsApprove() {
		return isApprove;
	}

	public void setIsApprove(String isApprove) {
		this.isApprove = isApprove;
	}

	/**
	 * @return the giftcardId
	 */
	public String getGiftcardId() {
		return giftcardId;
	}

	/**
	 * @param giftcardId the giftcardId to set
	 */
	public void setGiftcardId(String giftcardId) {
		this.giftcardId = giftcardId;
	}

	/**
	 * @return the giftcardItem
	 */
	public GiftcardItem getGiftcardItem() {
		return giftcardItem;
	}

	/**
	 * @param giftcardItem the giftcardItem to set
	 */
	public void setGiftcardItem(GiftcardItem giftcardItem) {
		this.giftcardItem = giftcardItem;
	}

}